using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Plugin.Sns.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml.Linq;
namespace MicroMsg.Manager
{
	public class QQMailMgr
	{
		private const string TAG = "QQMailMgr";
		public const int OBJID = 1;
		private EventWatcher QQMailModifyWatcher;
		public QQMailMgr()
		{
			this.init();
			ChatMsgMgr.registerCleanMsg(new Action(QQMailMgr.MailMsgClearAll));
		}
		private void init()
		{
			this.QQMailModifyWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onMailChange));
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_NEWSMSG_ADD, this.QQMailModifyWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_NEWSMSG_DEL, this.QQMailModifyWatcher);
		}
		private void uninit()
		{
			if (this.QQMailModifyWatcher != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_NEWSMSG_ADD, this.QQMailModifyWatcher);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_NEWSMSG_DEL, this.QQMailModifyWatcher);
				this.QQMailModifyWatcher = null;
			}
		}
		public static ObservableCollection<QQMail> getList(int start, int int_0)
		{
			List<QQMail> mailListFromChat = QQMailMgr.getMailListFromChat(start, int_0);
			if (mailListFromChat == null)
			{
				return null;
			}
			return new ObservableCollection<QQMail>(mailListFromChat);
		}
		public static List<QQMail> getMailListFromChat(int start, int int_0)
		{
			List<QQMail> list = new List<QQMail>();
			List<ChatMsg> msgList = StorageMgr.chatMsg.getMsgList("qqmail", start, int_0);
			if (msgList == null)
			{
				return null;
			}
			using (List<ChatMsg>.Enumerator enumerator = msgList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					QQMail qQMail = QQMailMgr.convertChatMsg2QQMail(current);
					if (qQMail != null)
					{
						list.Add(qQMail);
					}
				}
			}
			return list;
		}
		public static ObservableCollection<ChatMsg> getMailChatList(int start, int int_0)
		{
			List<ChatMsg> msgList = StorageMgr.chatMsg.getMsgList("qqmail", start, int_0);
			if (msgList == null)
			{
				return null;
			}
			if (msgList == null)
			{
				return null;
			}
			return new ObservableCollection<ChatMsg>(msgList);
		}
		public static QQMail convertChatMsg2QQMail(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 != null && chatMsg_0.strMsg != null)
			{
				if (chatMsg_0.nIsSender != 1)
				{
					if (chatMsg_0.strMsg == null)
					{
						return null;
					}
					string strMsg = chatMsg_0.strMsg;
					QQMail qQMail = QQMailMgr.parseMailXml(strMsg);
					if (qQMail == null && !strMsg.Contains("<"))
					{
						qQMail = new QQMail();
						qQMail.strDate = Util.formatTimeInChatMsg(chatMsg_0.nCreateTime);
						qQMail.strDescipt = chatMsg_0.strMsg;
					}
					if (qQMail != null)
					{
						qQMail.nMsgLocalID = chatMsg_0.nMsgLocalID;
					}
					return qQMail;
				}
			}
			return null;
		}
		public static snsMediaDownloadInfo getMailImg(string imgUrl)
		{
			snsMediaDownloadInfo snsMediaDownloadInfo = new snsMediaDownloadInfo();
			snsMediaDownloadInfo.priority = SnsPriority.HIGH;
			snsMediaDownloadInfo.urlType = SnsUrlType.MMSNS_URL_CDN;
			snsMediaDownloadInfo.snsObjID = 1uL;
			snsMediaDownloadInfo.string_0 = imgUrl;
			SnsMediaMgr.getImg(snsMediaDownloadInfo, true);
			return snsMediaDownloadInfo;
		}
		public static QQMail parseMailXml(string strXml)
		{
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			if (!strXml.StartsWith("<"))
			{
				Log.smethod_5("QQMailMgr", "strXml need cut");
				int num = strXml.IndexOf("<", 0);
				if (num < 0)
				{
					return null;
				}
				strXml = strXml.Substring(num);
			}
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			QQMail qQMail = new QQMail();
			QQMail result;
			try
			{
				XDocument xDocument = XDocument.Parse(strXml);
				XElement xElement = xDocument.Element("msg");
				if (xElement != null)
				{
					XElement xElement2 = xElement.Element("pushmail");
					if (xElement2 != null)
					{
						XElement xElement3 = xElement2.Element("mailid");
						if (xElement3 != null)
						{
							qQMail.strMailid = xElement3.get_Value();
						}
						XElement xElement4 = xElement2.Element("waplink");
						if (xElement4 != null)
						{
							qQMail.strUrl = xElement4.get_Value();
						}
						XElement xElement5 = xElement2.Element("content");
						if (xElement5 != null)
						{
							XElement xElement6 = xElement5.Element("subject");
							if (xElement6 != null)
							{
								qQMail.strTitle = xElement6.get_Value();
							}
							XElement xElement7 = xElement5.Element("attach");
							if (xElement7 != null)
							{
								qQMail.hasAttach = bool.Parse(xElement7.get_Value());
							}
							XElement xElement8 = xElement5.Element("date");
							if (xElement8 != null)
							{
								qQMail.strDate = xElement8.get_Value();
							}
							XElement xElement9 = xElement5.Element("sender");
							if (xElement9 != null)
							{
								qQMail.strSender = xElement9.get_Value();
							}
							XElement xElement10 = xElement5.Element("digest");
							if (xElement10 != null)
							{
								qQMail.strDescipt = xElement10.get_Value();
							}
							XElement xElement11 = xElement5.Element("fromlist");
							if (xElement11 != null)
							{
								if (qQMail.fromList == null)
								{
									qQMail.fromList = new List<MailContactItem>();
								}
								qQMail.fromList.Clear();
								using (IEnumerator<XElement> enumerator = xElement11.Elements().GetEnumerator())
								{
									while (enumerator.MoveNext())
									{
										XElement current = enumerator.get_Current();
										if (current != null)
										{
											MailContactItem mailContactItem = new MailContactItem();
											XElement xElement12 = current.Element("name");
											if (xElement12 != null)
											{
												mailContactItem.strName = xElement9.get_Value();
											}
											XElement xElement13 = current.Element("addr");
											if (xElement13 != null)
											{
												mailContactItem.strAddress = xElement13.get_Value();
											}
											qQMail.fromList.Add(mailContactItem);
										}
									}
								}
							}
							XElement xElement14 = xElement5.Element("fromlist");
							if (xElement14 != null)
							{
								if (qQMail.toList == null)
								{
									qQMail.toList = new List<MailContactItem>();
								}
								qQMail.toList.Clear();
								using (IEnumerator<XElement> enumerator2 = xElement14.Elements().GetEnumerator())
								{
									while (enumerator2.MoveNext())
									{
										XElement current2 = enumerator2.get_Current();
										if (current2 != null)
										{
											MailContactItem mailContactItem2 = new MailContactItem();
											XElement xElement15 = current2.Element("name");
											if (xElement15 != null)
											{
												mailContactItem2.strName = xElement9.get_Value();
											}
											XElement xElement16 = current2.Element("addr");
											if (xElement16 != null)
											{
												mailContactItem2.strAddress = xElement16.get_Value();
											}
											qQMail.toList.Add(mailContactItem2);
										}
									}
								}
							}
							XElement xElement17 = xElement2.Element("cclist");
							if (xElement17 != null)
							{
								if (qQMail.ccList == null)
								{
									qQMail.ccList = new List<MailContactItem>();
								}
								qQMail.ccList.Clear();
								using (IEnumerator<XElement> enumerator3 = xElement17.Elements().GetEnumerator())
								{
									while (enumerator3.MoveNext())
									{
										XElement current3 = enumerator3.get_Current();
										if (current3 != null)
										{
											MailContactItem mailContactItem3 = new MailContactItem();
											XElement xElement18 = current3.Element("name");
											if (xElement18 != null)
											{
												mailContactItem3.strName = xElement9.get_Value();
											}
											XElement xElement19 = current3.Element("addr");
											if (xElement19 != null)
											{
												mailContactItem3.strAddress = xElement19.get_Value();
											}
											qQMail.ccList.Add(mailContactItem3);
										}
									}
								}
							}
						}
						result = qQMail;
						return result;
					}
				}
				result = null;
			}
			catch (Exception ex)
			{
				Log.smethod_1("QQMailMgr", ex.ToString());
				result = null;
			}
			return result;
		}
		public static int getMailCountFromeXml(string strXml)
		{
			if (string.IsNullOrEmpty(strXml))
			{
				return 0;
			}
			if (!strXml.StartsWith("<"))
			{
				Log.smethod_5("QQMailMgr", "strXml need cut");
				int num = strXml.IndexOf("<", 0);
				if (num < 0)
				{
					return 0;
				}
				strXml = strXml.Substring(num);
			}
			if (string.IsNullOrEmpty(strXml))
			{
				return 0;
			}
			int num2 = 0;
			int result;
			try
			{
				XDocument xDocument = XDocument.Parse(strXml);
				XElement xElement = xDocument.Element("mmreader");
				if (xElement != null)
				{
					XElement xElement2 = xElement.Element("category");
					if (xElement2 != null)
					{
						using (IEnumerator<XElement> enumerator = xElement2.Elements().GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								XElement current = enumerator.get_Current();
								if (current != null && (current.get_Name() == "item" || current.get_Name() == "newitem"))
								{
									XElement xElement3 = current.Element("title");
									if (xElement3 != null)
									{
										num2++;
									}
								}
							}
						}
						result = num2;
						return result;
					}
				}
				result = 0;
			}
			catch (Exception ex)
			{
				Log.smethod_1("QQMailMgr", ex.ToString());
				result = 0;
			}
			return result;
		}
		private List<ChatMsg> getMailMsgList(BaseEventArgs evtArgs)
		{
			List<ChatMsg> listObject = evtArgs.getListObject<ChatMsg>();
			if (listObject == null)
			{
				return null;
			}
			List<ChatMsg> list = null;
			using (List<ChatMsg>.Enumerator enumerator = listObject.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					if (ContactHelper.isQQMailContact(current.strTalker))
					{
						if (list == null)
						{
							list = new List<ChatMsg>();
						}
						list.Add(current);
					}
				}
			}
			return list;
		}
		private void onMailChange(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.getListObject<QQMail>() == null)
			{
				return;
			}
			EventCenter.postEvent(EventConst.ON_NEWS_LIST_CHANGE, evtArgs.mObject, null);
		}
		private void ChatUpdateMsg(ChatMsg chatMsg_0, bool isAddMsg = false, int combinMsgCount = 1)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			chatMsg_0.IsSupport();
		}
		public static void UpdateConversationFromMsg(ChatMsg chatMsg_0, QQMail conv, bool isNewMsg)
		{
			if (chatMsg_0 != null && conv != null)
			{
				conv.nMsgLocalID = chatMsg_0.nMsgLocalID;
				return;
			}
		}
		public static void delMainConversation()
		{
			ConversationMgr.delConversation(ConstantValue.TAG_QQMAIL);
		}
		public static int getUnReadCountAll()
		{
			List<QConversation> list = StorageMgr.qconv.getList();
			if (list != null && list.get_Count() > 0)
			{
				int num = 0;
				using (List<QConversation>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						QConversation current = enumerator.get_Current();
						num += current.nUnReadCount;
					}
				}
				return num;
			}
			return 0;
		}
		public static void MailMsgClearAll()
		{
			StorageMgr.chatMsg.delByTalker(ConstantValue.TAG_QQMAIL);
			ConversationMgr.delConversation(ConstantValue.TAG_QQMAIL);
		}
		public static void test()
		{
			for (int i = 0; i < 2; i++)
			{
				ChatMsg chatMsg = new ChatMsg();
				chatMsg.nMsgType = 1;
				chatMsg.nMsgSvrID = 35;
				chatMsg.strTalker = "newsapp";
				chatMsg.strMsg = "<mmreader><category type=\"20\" count=\"1\"><name><![CDATA[腾讯邮件]]></name><topnew><cover><![CDATA[http://pnewsapp.tc.qq.com/newsapp_ls/0/11511_small/0]]></cover><width><![CDATA[640]]></width><height><![CDATA[330]]></height><digest><![CDATA[http://img1.gtimg.com/mail/pics/hv1/196/31/1026/66723751.jpghttp://img1.gtimg.com/mail/pics/hv1/195/31/1026/66723750.jpg&lt;p&gt;4&#x6708;24&#x65E5;&#xFF0C;&#x5927;&#x8FDE;&#x5468;&#x6C34;&#x5B50;&#x56FD;&#x9645;&#x673A;&#x573A;&#x56E0;&#x5927;&#x96FE;&#x6EDE;&#x7559;&#x65C5;&#x5BA2;5000&#x4F59;&#]]></digest></topnew><item><title><![CDATA[大连机场5千人滞留 美女热舞安抚]]></title><url><![CDATA[http://yd.weixin.qq.com/reader/20/0B6fnV?c=50&n=0&id=12398&aid=WXI2012042500120010&mmuin=1103743700&cv={CLIVER}]]></url><shorturl><![CDATA[http://url.cn/0B6fnV]]></shorturl><longurl><![CDATA[http://weixin.qq.com/a/20120425/001200.htm]]></longurl><pub_time>1335353454</pub_time><cover><![CDATA[http://pnewsapp.tc.qq.com/newsapp_ls/0/11511_small/0|640|330]]></cover><tweetid>I2012042500120010</tweetid><sources><source><name><![CDATA[版权图片，请勿转载]]></name></source></sources></item></category></mmreader>";
				StorageMgr.chatMsg.addMsg(chatMsg);
			}
		}
	}
}
